package pers.li.controller.order;

import pers.li.model.TO.OrderTO;
import pers.li.service.OrderService;
import pers.li.utils.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/order/commitOrder.action")
public class CommitOrder extends HttpServlet {
    private final OrderService orderService = new OrderService();

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader br = req.getReader();
        //2.读取数据
        String line = null;
        String body = null;
        while ((line = br.readLine()) != null) {
            body = line;
        }
        OrderTO orderTO = JsonUtil.parseObject(body, OrderTO.class);
        orderService.insertOrder(orderTO);
        req.getSession().removeAttribute("car");
        req.getSession().removeAttribute("carJson");
        Map<String, Object> res = new HashMap<>();
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        res.put("success", true);
        writer.print(JsonUtil.toJsonString(res));
        writer.flush();
        writer.close();
    }
}
